package com.ewzj.bigdata.mvp.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.utils.RegexUtils;
import com.ewzj.bigdata.di.component.DaggerLoginFragmentComponent;
import com.ewzj.bigdata.di.module.LoginFragmentModule;
import com.ewzj.bigdata.mvp.contract.LoginFragmentContract;
import com.ewzj.bigdata.mvp.presenter.LoginFragmentPresenter;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginFragmentFragment extends BaseFragment<LoginFragmentPresenter> implements LoginFragmentContract.View {


    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    public static LoginFragmentFragment newInstance() {
        LoginFragmentFragment fragment = new LoginFragmentFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerLoginFragmentComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginFragmentModule(new LoginFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initComposbale();
    }

    private void initComposbale() {
        Observable<CharSequence> _phoneChangeObservable = RxTextView.textChanges(etUsername).skip(1);
        Observable<CharSequence> _passwordChangeObservable = RxTextView.textChanges(etPassword).skip(1);
        Observable.combineLatest(_phoneChangeObservable, _passwordChangeObservable, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence charSequence, CharSequence charSequence2) throws Exception {
                Boolean isphone= RegexUtils.checkPhone(charSequence.toString());
                Boolean isempty= RegexUtils.checkPassword(charSequence2.toString());
                return isphone&&isempty;
            }
        }).subscribe(new ErrorHandleSubscriber<Boolean>(ArmsUtils.obtainAppComponentFromContext(getActivity()).rxErrorHandler()) {
            @Override
            public void onNext(Boolean aBoolean) {
                if (aBoolean){
                    tvLogin.setEnabled(true);
                    tvLogin.setSelected(true);
                }else {
                    tvLogin.setEnabled(false);
                    tvLogin.setSelected(false);
                }
            }
        });
    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @OnClick({R.id.tv_forgort, R.id.iv_qq, R.id.iv_sina, R.id.iv_wechat, R.id.tv_login})
    public void onViewClicked(View view) {
        String strphone=etUsername.getText().toString();
        String strpass=etPassword.getText().toString();
        switch (view.getId()) {
            case R.id.tv_forgort:
                break;
            case R.id.iv_qq:
                mPresenter.initThirdLogin(SHARE_MEDIA.QQ);
                break;
            case R.id.iv_sina:
                mPresenter.initThirdLogin(SHARE_MEDIA.SINA);
                break;
            case R.id.iv_wechat:
                mPresenter.initThirdLogin(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.tv_login:
                login(strphone,strpass);
                break;
        }
    }
    private void login(String phone,String pass){
        if (TextUtils.isEmpty(phone)){
            ArmsUtils.snackbarText(getString(R.string.empty_phone));
            return;
        }
        if (!RegexUtils.checkMobile(phone)){
            ArmsUtils.snackbarText(getString(R.string.unright_phone));
            return;
        }
        if (TextUtils.isEmpty(pass)){
            ArmsUtils.snackbarText(getString(R.string.empty_pass));
            return;
        }
        if (!RegexUtils.checkPassword(phone)){
            ArmsUtils.snackbarText(getString(R.string.unright_pass));
            return;
        }
        mPresenter.login(phone,pass);
    }
    @Override
    public void loginSuccess() {
        getActivity().finish();
    }

    @Override
    public Activity getCurrentActivity() {
        return this.getActivity();
    }

    @Override
    public void showloadingDialog() {

    }

    @Override
    public void dismisloadingDialog() {

    }
}
